package sz.szsmk.citizencard.mvp.presenter;

import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.CollectedBusLineReq;
import sz.szsmk.citizencard.model.network.bean.request.QueryBusRouteReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusRouteRes;
import sz.szsmk.citizencard.mvp.contract.IRealTimeLineContract;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/21 14:41
 * description:
 */
public class RealTimePresenter extends IBasePresenter
        implements IRealTimeLineContract.IRealTimeLinePresenter<IRealTimeLineContract.IRealTimeLineView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public RealTimePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getRealLineData(String lineGuid, String line_name) {
        QueryBusRouteReq queryBusRouteReq = new QueryBusRouteReq();
        queryBusRouteReq.setLatitude("string");
        queryBusRouteReq.setLongitude("string");
        queryBusRouteReq.setLine_guid(lineGuid);
        queryBusRouteReq.setLine_name(line_name);
        queryBusRouteReq.setUserID("string");
        String json = new Gson().toJson(queryBusRouteReq);
        mNetworkDateSource.queryBusRoute(queryBusRouteReq, new SubscribeResult<QueryBusRouteRes>() {
            @Override
            protected void onOk(QueryBusRouteRes response) {
                String json = new Gson().toJson(response);
                ((IRealTimeLineContract.IRealTimeLineView) mView).getRealLineDataSucc(response);
            }

            @Override
            protected void onFailure(QueryBusRouteRes response) {
                super.onFailure(response);
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void cancelCollec(String line_guid, String line_name) {
        CollectedBusLineReq lineReq = new CollectedBusLineReq();
        lineReq.setLine_guid(line_guid);
        lineReq.setLine_name(line_name);
        String json = new Gson().toJson(lineReq);
        mNetworkDateSource.cancelCollectedBusLine(lineReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IRealTimeLineContract.IRealTimeLineView) mView).cancelCollecSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IRealTimeLineContract.IRealTimeLineView) mView).cancelCollecFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void addCollec(String line_guid, String line_name) {
        CollectedBusLineReq lineReq = new CollectedBusLineReq();
        lineReq.setLine_guid(line_guid);
        lineReq.setLine_name(line_name);
        String json = new Gson().toJson(lineReq);
        mNetworkDateSource.collectedBusLine(lineReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IRealTimeLineContract.IRealTimeLineView) mView).addCollecSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IRealTimeLineContract.IRealTimeLineView) mView).addCollecFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }


}
